﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Collections;

namespace TeamResourceManageAssist
{
    public class ProjectItem : ObservableCollection<TaskItem>
    {
        private DateTime _StartDate;
        private DateTime _EndDate;
        private bool Initialized
        { get; set; }

        public ProjectItem()
        {
            CollectionChanged += new NotifyCollectionChangedEventHandler(ProjectItem_CollectionChanged);
            Initialized = false;
        }

        [TypeConverter(typeof(DateTimeTypeConverter))]
        public DateTime StartDate
        {
            get { return _StartDate; }
            set
            {
                if (value.DayOfWeek != DayOfWeek.Sunday)
                {
                    value = value.AddDays(-Convert.ToDouble(value.DayOfWeek));
                }
                if (!Initialized || value.Date < _StartDate)
                {
                    _StartDate = value.Date;
                    OnPropertyChanged(new PropertyChangedEventArgs("StartDate"));
                }
            }
        }

        [TypeConverter(typeof(DateTimeTypeConverter))]
        public DateTime EndDate
        {
            get { return _EndDate; }
            set
            {
                if (value.DayOfWeek != DayOfWeek.Saturday)
                {
                    value = value.AddDays(Convert.ToDouble(DayOfWeek.Saturday - value.DayOfWeek));
                }
                if (!Initialized || value.Date > _EndDate)
                {
                    _EndDate = value.Date;
                    OnPropertyChanged(new PropertyChangedEventArgs("EndDate"));
                }
            }
        }

        void ProjectItem_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            DateTime SmallestDate = new DateTime();
            DateTime LargestDate = new DateTime();
            bool FirstItem = true;
            IList tasks;
            if (e.Action == NotifyCollectionChangedAction.Add)
                tasks = e.NewItems;
            else
                tasks = this;

            foreach (TaskItem task in tasks)
            {
                if (FirstItem)
                {
                    SmallestDate = task.start_time.Date;
                    LargestDate = task.end_time.Date;
                    FirstItem = false;
                }
                else
                {
                    if (task.start_time.Date < SmallestDate)
                        SmallestDate = task.start_time.Date;
                    if (task.end_time.Date > LargestDate)
                        LargestDate = task.end_time.Date;
                }
            }
            if (FirstItem == false)
            {
                StartDate = SmallestDate;
                EndDate = LargestDate;
                Initialized = true;
            }
        }
    }
}
